﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SWBetongXaydung.GUI.Common;
using SWBetongXaydung.BLL;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;
using SWBetongXaydung.Utils;
using QldtSdh.Utils;
using System.Threading;
using System.IO;
namespace SWBetongXaydung.GUI.ThongKe
{
    public partial class UCThongKe_ThanhToanNhienLieu : BaseUserControl
    {
        bool allowUpdate = false;
        private string filePathTemp = Application.StartupPath + "\\TEMP\\{0}.doc";
        private int timeToWaitExport = 4000;
        WordUtil word = null;
        DAL.BETONGXAYDUNGEntities _dbContext = null;
        public DAL.BETONGXAYDUNGEntities DBContext
        {
            get
            {
                if (_dbContext == null)
                    _dbContext = DAL.DBContextHelper.GetContext();
                return _dbContext;
            }
        }
        public UCThongKe_ThanhToanNhienLieu()
        {
            InitializeComponent();
            allowUpdate = false;
            cmbThang.Text = DateTime.Now.Month.ToString();
            cmbNam.Text = DateTime.Now.Year.ToString();
            LoadXe();
            LoadTaiXe();
            allowUpdate = true;
        }
        void LoadXe()
        {
            cmbSoXe.DataSource = XeBLL.GetList();
            cmbSoXe.DisplayMember = "SoXe";
            cmbSoXe.ValueMember = "SoXe";
        }
        void LoadTaiXe()
        {
            cmbTaiXe.DataSource = TaiXeBLL.GetList();
            cmbTaiXe.DisplayMember = "TenDayDu";
            cmbTaiXe.ValueMember = "IDTaiXe";
            if (cmbSoXe.SelectedIndex != -1)
            {
                var pc = PhanCongLaiXeBLL.Get(cmbSoXe.SelectedValue.ToString());
                if (pc != null)
                    if (cmbTaiXe.SelectedIndex != -1)
                        cmbTaiXe.SelectedValue = pc.IDTaiXe;
            }
        }
        bool Validate_Data()
        {
            errorNotify.Clear();
            int i = 0;
            if (!int.TryParse(cmbThang.Text, out i))
            {
                errorNotify.SetError(cmbThang, "Tháng không đúng");
                return false;
            }
            if (!int.TryParse(cmbNam.Text, out i))
            {
                errorNotify.SetError(cmbNam, "Năm không đúng");
                return false;
            } if (cmbSoXe.SelectedIndex == -1)
            {
                errorNotify.SetError(cmbSoXe, "Bạn chưa chọn xe");
                return false;
            }
            if (cmbTaiXe.SelectedIndex == -1)
            {
                errorNotify.SetError(cmbTaiXe, "Bạn chưa chọn tài xế");
                return false;
            }

            return true;
        }

        private void cmbSoXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                if (cmbSoXe.SelectedIndex != -1)
                {
                    var pc = PhanCongLaiXeBLL.Get(cmbSoXe.SelectedValue.ToString());
                    if (pc != null)
                        if (cmbTaiXe.SelectedIndex != -1)
                            cmbTaiXe.SelectedValue = pc.IDTaiXe;
                }
            }
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            if (Validate_Data())
            {
                int IDTaiXe = int.Parse(cmbTaiXe.SelectedValue.ToString());
                string soXe = cmbSoXe.Text;
                int thang = int.Parse(cmbThang.Text);
                int nam = int.Parse(cmbNam.Text);
                report(IDTaiXe, soXe, thang, nam);
            }
        }
        //TYPE_REPORT GetLoaiThongKe()
        //{
        //    TYPE_REPORT loaiThongKe = TYPE_REPORT.BinhThuong;
        //    if (rdBinhThuong.Checked)
        //        loaiThongKe = TYPE_REPORT.BinhThuong;
        //    else if (rdCanDoi.Checked)
        //        loaiThongKe = TYPE_REPORT.CanDoi;
        //    else
        //        loaiThongKe = TYPE_REPORT.Thue;
        //    return loaiThongKe;
        //}
        void report(int IDTaiXe, string soXe, int thang, int nam, bool preview = true, bool printMultifile = false)
        {
            var taiXe = TaiXeBLL.GetTaiXe(IDTaiXe);
            var donGia = DonGiaNhienLieuBLL.GetDonGia(thang, nam, soXe);
            var listCapNhienLieu = ThongKeCapNhienLieu.GetList(thang, nam, IDTaiXe, soXe);
            var listNhienLieuTieuThu = ThongKeNhienLieuTieuThu.GetList(thang, nam, IDTaiXe, soXe, TYPE_REPORT.BinhThuong);
            ThongKeCapNhienLieuBindingSource.DataSource = listCapNhienLieu;
            ThongKeNhienLieuTieuThuBindingSource.DataSource = listNhienLieuTieuThu;
            var tongTienCapNhienLieu = (listCapNhienLieu != null && listCapNhienLieu.Count > 0) ? listCapNhienLieu.Sum(t => t.ThanhTien) : 0;
            var tongTienNhienLieuTieuThu = (listNhienLieuTieuThu != null && listNhienLieuTieuThu.Count > 0) ? listNhienLieuTieuThu.Sum(t => t.ThanhTien) : 0;
            ReportParameter[] parameters = new ReportParameter[6];
            parameters[0] = new ReportParameter("rpThang", "THÁNG " + cmbThang.Text + "/" + cmbNam.Text);
            parameters[1] = new ReportParameter("rpTaiXe", "Tài xế: " + ((taiXe != null) ? taiXe.TenDayDu : ""));
            parameters[2] = new ReportParameter("rpSoXe", "Số xe: " + soXe);
            parameters[3] = new ReportParameter("rpDonGiaStr", "Đơn giá: " + (donGia == null ? "0" : donGia.DonGia.ToString().FormatCurrency()) + "đ");
            parameters[4] = new ReportParameter("rpDonGia", (donGia == null ? 0 : donGia.DonGia).ToString());
            parameters[5] = new ReportParameter("rpTienBangChu", "Bằng chữ: " +
                Math.Floor(Math.Abs(tongTienNhienLieuTieuThu - tongTienCapNhienLieu)).ToString().ConvertNum2Text() + "đồng");
            System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
            pg.Margins.Top = 20;
            pg.Margins.Bottom = 20;
            pg.Margins.Left = 20;
            pg.Margins.Right = 20;
            pg.Landscape = false;
            PaperSize size = new PaperSize();
            size.Width = 827;
            size.Height = 1169;
            pg.PaperSize = size;
            rpThanhToanNhienLieu.SetPageSettings(pg);
            rpThanhToanNhienLieu.LocalReport.SetParameters(parameters);
            rpThanhToanNhienLieu.RefreshReport();
            //rpThanhToanNhienLieu.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            if (!preview)
            {
                rpThanhToanNhienLieu.PrinterSettings.PrinterName = PrintUtil.GetDefaultPrinter();
                string fileName = string.Format(filePathTemp, IDTaiXe);
                LocalReport report = this.rpThanhToanNhienLieu.LocalReport;
                PrintUtil.Export(report, PrintUtil.WORDFORMAT, fileName);
                Thread.Sleep(timeToWaitExport);
                word = new WordUtil(fileName);
                word.PrintDocument(true);
                if (!printMultifile)
                    word.EndWork();
            }
        }

        private void btnInTatCa_Click(object sender, EventArgs e)
        {
            if (Validate_Data())
            {
                //var loaiThongKe = GetLoaiThongKe();
                int thang = int.Parse(cmbThang.Text);
                int nam = int.Parse(cmbNam.Text);
                var listTaiXe = TaiXeBLL.GetList(DBContext);
                if (listTaiXe != null && listTaiXe.Count > 0)
                    foreach (var item in listTaiXe)
                    {
                        var listDieuXe = DieuXeBLL.GetList(item.IDTaiXe);
                        if (listDieuXe != null && listDieuXe.Count > 0)
                        {
                            foreach (var dieuXe in listDieuXe)
                                report(item.IDTaiXe, dieuXe, thang, nam, false, true);
                        }
                        else
                        {
                            var pc = PhanCongLaiXeBLL.Get(item.IDTaiXe);
                            if (pc != null)
                                report(item.IDTaiXe, pc.SoXe, thang, nam, false, true);
                        }
                    }
            }
        }

        private void UCThongKe_ThanhToanNhienLieu_ControlRemoved(object sender, ControlEventArgs e)
        {
            if (_dbContext != null)
                _dbContext.Dispose();
        }
    }
}
